// store命名
import { StoreName } from '@store/store-name';

type Process = {
    orderId: string;
    orderNo: string;
    orderDate: string;
    orderDeliveryDate: string;
    customerId: string;
    productName: string;
    productId: string;
    orderNumber: number;
    orderRemark: string;
    creator: string;
    creatorId: string;
};
// 用户模块
export const useOrderStore = defineStore(StoreName.ORDER, {
    state: (): {
        process: Process[];
    } => ({
        process: [],
    }),
    actions: {
        async addOrderProcessList(
            orderId: string,
            customerId: string,
            orderNo: string,
            orderDate: string,
            orderDeliveryDate: string,
            data: AjaxResponse['orderList'][0]['orderDetail'],
            orderRemark: string,
            creator: string,
            creatorId: string
        ) {
            data.forEach((element) => {
                this.process.push({
                    orderId: orderId,
                    orderNo: orderNo,
                    orderDate: orderDate,
                    orderDeliveryDate: orderDeliveryDate,
                    customerId: customerId,
                    productName: element.productName,
                    productId: element.productId,
                    orderNumber: element.orderNumber,
                    orderRemark: orderRemark,
                    creator: creator,
                    creatorId: creatorId,
                });
            });
        },
        async deleteOrderProcess(orderNo: string, productName: string) {
            let count = 0;
            for (let index = 0; index < this.process.length; index++) {
                const process = this.process[index];
                if (process.orderNo === orderNo && process.productName === productName) {
                    this.process.splice(index, 1);
                    count++;
                    return;
                }
            }
            if (count === 0) {
                ElMessage({
                    message: '更改产品名称后，需要手动删除待处理订单。',
                    type: 'warning',
                });
            }
        },
        async removeOrderProcess(orderId: string) {
            this.process = this.process.filter((item) => item.orderId !== orderId);
        },
    },
    persist: {
        storage: localStorage,
    },
});
